//
//  FSLWaterfallLayout.swift
//  瀑布流布局
//
//  Created by 我演示 on 2019/11/4.
//  Copyright © 2019 ALLSall. All rights reserved.
//

import UIKit


protocol FSLWaterfallLayoutDataSource : class  {
    func numberOfCols (_ waterfallLayout : FSLWaterfallLayout ) -> Int
    func waterfall(_ waterfall : FSLWaterfallLayout , item : Int)  -> CGFloat
}

class FSLWaterfallLayout: UICollectionViewFlowLayout {
    
    weak var dataSoutce : FSLWaterfallLayoutDataSource?
    
    fileprivate lazy var cellAttrs : [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
  // 定义一个默认的初始值信息 cell 的位置的数据
    fileprivate lazy   var totalHeights : [CGFloat ] = Array(repeating: self.sectionInset.top, count: 3)
       
    fileprivate lazy var cols : Int = {
        return self.dataSoutce?.numberOfCols(self) ?? 2
    }()
}

// 准备布局
extension FSLWaterfallLayout {
      
        override func prepare() {
            super.prepare()
            
     
            
    //        UICollectionViewLayoutAttributes  最终的布局有这个属性来确定
            
            let itemContent = collectionView!.numberOfItems(inSection: 0)
            
            let cols = 3
            
            //2: 获取cell 的布局创建信息
            for i in 0..<itemContent {
                //1: 根据i创建itemSize
                
                let indexPath = IndexPath(item: 0, section: 0)
                
                //2: 根据indexPath创建UICollectionViewLayoutAttributes
                let attrr = UICollectionViewLayoutAttributes(forCellWith: indexPath)
                
                guard let cellH : CGFloat = dataSoutce?.waterfall(self, item: i ) else {
                    return
                }
                
                var minHeight = totalHeights.min()!
                
                let minIndex = totalHeights.index(of: minHeight)!
                
               
                
                let cellW : CGFloat = (collectionView!.bounds.width - sectionInset.left - sectionInset.right - CGFloat(cols - 1) * minimumInteritemSpacing  ) / CGFloat(cols)
                let cellX : CGFloat = sectionInset.left + (minimumInteritemSpacing + cellW) * CGFloat(minIndex)
                let cellY : CGFloat =  minHeight + minimumLineSpacing
                
                //3: 添加frame
                attrr.frame = CGRect(x: cellX, y: cellY, width: cellW, height: cellH)
            
                /// tian添加上我们的高度
                totalHeights [minIndex] = minHeight + minimumLineSpacing + cellH
                
                //4: 保存attr
                cellAttrs.append(attrr)
                
            }
            
            
        }
        

}

/// 返回准备好的布局
extension FSLWaterfallLayout {
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.cellAttrs
    }
}


//MARK- 设置contentSIze
extension FSLWaterfallLayout {
    override var collectionViewContentSize: CGSize {
        
        return CGSize(width: 0, height: totalHeights.max()! + sectionInset.bottom )
    }
    
}
